package com.mylib.libcore.utils;

import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditTextUtil {

    /**
     * 过滤非选中输入，比如输入中文时，过滤掉搜索框里已经输入的拼音（小米手机出现的情况）
     * 返回空字符串，就代表匹配不成功，返回null代表匹配成功
     */

    public static InputFilter getPinyinInputFilter() {
        InputFilter iF = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                SpannableString ss = new SpannableString(source);
                Object[] spans = ss.getSpans(0, ss.length(), Object.class);
                if (spans != null) {
                    for (Object span : spans) {
                        if (span instanceof UnderlineSpan) {
                            return "";
                        }
                    }
                }
                return null;
            }
        };

        return iF;
    }

    /**
     * 设置inputFilter后，会导致maxLength失效
     * 需要设置新的maxLengthfilter
     *
     * @param maxLength
     * @return
     */
    public static InputFilter getMaxLengthInputFilter(int maxLength) {
        return new LengthFilter(maxLength);
    }

    /**
     * 限制小数点后位数
     *
     * @param decimalDigits
     * @return
     */
    public static InputFilter getDecimalDigitsInputFilter(int decimalDigits) {
        return new DecimalDigitsInputFilter(decimalDigits);
    }


    /**
     * 限制输入emoji表情
     *
     * @return
     */
    public static InputFilter getDisableEmojiFilter() {
        InputFilter emojiFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83e\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    return "";
                }
                return null;
            }
        };
        return emojiFilter;
    }


    public static class LengthFilter implements InputFilter {
        private final int mMax;

        public LengthFilter(int max) {
            mMax = max;
        }


        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                   int dstart, int dend) {
            int keep = mMax - (dest.length() - (dend - dstart));
            if (keep <= 0) {
                return "";
            } else if (keep >= end - start) {
                return null; // keep original
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }

        /**
         * @return the maximum length enforced by this input filter
         */
        public int getMax() {
            return mMax;
        }
    }


    /**
     * 显示输入框中小数点后的位数
     */
    public static class DecimalDigitsInputFilter implements InputFilter {

        private final int decimalDigits;

        /**
         * Constructor.
         *
         * @param decimalDigits maximum decimal digits
         */
        public DecimalDigitsInputFilter(int decimalDigits) {
            this.decimalDigits = decimalDigits;
        }

        @Override
        public CharSequence filter(CharSequence source,
                                   int start,
                                   int end,
                                   Spanned dest,
                                   int dstart,
                                   int dend) {

            if (source.equals(".") && dest.toString().length() == 0) {
                return "0.";
            }
            if (dest.toString().contains(".")) {
                int index = dest.toString().indexOf(".");
                int length = dest.toString().substring(index).length();
                if (length == decimalDigits + 1) {
                    return "";
                }
            }

            return null;
        }

    }

}
